//
//  ContactManager.m
//  Showing
//
//  Created by Ralf on 3/28/15.
//  Copyright (c) 2015 next-software. All rights reserved.
//

#import "ContactManager.h"
#import "AppDelegate.h"

@implementation ContactManager

@synthesize contact;

- (ResultSet *)queryBySessionID
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];

    return [self request:@"contact/queryContactBySessionID" params:params];
}

- (ResultSet *)loadContact
{
    ResultSet *rs = [self queryBySessionID];

    if (rs && rs.statusCode == 200)
    {
        self.contact = rs.toDictionary;
    }

    return rs;
}

- (ResultSet *)queryByUserName:(NSString *)userName
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:userName forKey:@"userName"];

    return [self request:@"contact/queryContactByUserName" params:params];
}

- (ResultSet *)uploadAvatar:(UIImage *)image
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    params[@"sessionID"] = [self getSessionID];

    NSData *imageData = UIImageJPEGRepresentation(image, 0.6);

    NSString *imageString = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];//[imageData base64Encoding];
    [params setValue:imageString forKey:@"image"];

    return [self requestJSON:@"contact/uploadAvatar" params:params];
}

- (ResultSet *)uploadBackground:(UIImage *)image
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    params[@"sessionID"] = [self getSessionID];

    NSData   *imageData   = UIImageJPEGRepresentation(image, 0.6);
    NSString *imageString = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];//[imageData base64Encoding];
    [params setValue:imageString forKey:@"image"];

    return [self requestJSON:@"contact/uploadBackground" params:params];
}

- (void)logout
{
    self.contact = nil;
    AppDelegate *appDelegate = [self getAppDelegate];
    appDelegate.parameterManager.sessionID = nil;
    [appDelegate.parameterManager save];
}

- (ResultSet *)queryFollowing:(NSInteger)max offset:(NSInteger)offset
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:[[NSString alloc] initWithFormat:@"%ld", (long) max] forKey:@"max"];
    [params setValue:[[NSString alloc] initWithFormat:@"%ld", (long) offset] forKey:@"offset"];

    return [self request:@"contact/queryFollowing" params:params];
}

- (ResultSet *)queryTopContact:(NSInteger)max offset:(NSInteger)offset
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[[NSString alloc] initWithFormat:@"%ld", (long) max] forKey:@"max"];
    [params setValue:[[NSString alloc] initWithFormat:@"%ld", (long) offset] forKey:@"offset"];

    if ([[self getAppDelegate] isLogged])
    {
        [params setValue:[self getSessionID] forKey:@"sessionID"];
    }

    return [self request:@"contact/queryTopContact" params:params];
}

- (ResultSet *)addFollowing:(NSString *)userName
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:userName forKey:@"userName"];

    return [self request:@"contact/addFollowing" params:params];
}

- (ResultSet *)isFollowing:(NSString *)userName
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:userName forKey:@"userName"];

    return [self request:@"contact/isFollowing" params:params];
}

- (ResultSet *)searchByAlias:(NSString *)alias
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:alias forKey:@"alias"];

    return [self request:@"contact/searchByAlias" params:params];
}

- (ResultSet *)updateProfile:(NSDictionary *)profile
{
    NSDictionary *params = [NSMutableDictionary dictionaryWithDictionary:profile];

    [params setValue:[self getSessionID] forKey:@"sessionID"];

    return [self request:@"contact/updateProfile" params:params];
}

- (ResultSet *)updatePassword:(NSDictionary *)passwords
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:passwords[@"oldPassword"] forKey:@"oldPassword"];
    [params setValue:passwords[@"newPassword"] forKey:@"newPassword"];
    return [self request:@"contact/changePassword" params:params];
}

- (ResultSet *)queryFollower:(NSInteger)max offset:(NSInteger)offset
{
    NSDictionary *params = [NSMutableDictionary new];
    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:[[NSString alloc] initWithFormat:@"%ld", (long) max] forKey:@"max"];
    [params setValue:[[NSString alloc] initWithFormat:@"%ld", (long) offset] forKey:@"offset"];

    return [self request:@"contact/queryFollower" params:params];
}

- (ResultSet *)loginWithUserName:(NSString *)userName andPassword:(NSString *)password
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:userName forKey:@"userName"];
    [params setValue:password forKey:@"password"];
    NSString *identifier = [ParameterManager GetDeviceToken];
    [params setValue:identifier forKey:@"deviceTokens"];
    ResultSet *rs = [self request:@"contact/login" params:params];

    if (rs == nil || [rs hasErrors] || [rs isEmpty])
    {

    }
    else
    {
        self.contact = [rs toDictionary];
        AppDelegate *appDelegate = [self getAppDelegate];
        appDelegate.parameterManager.sessionID = self.contact[@"sessionID"];
        [appDelegate.parameterManager save];
    }
    return rs;
}

- (ResultSet *)registerWithUserName:(NSString *)userName andPassword:(NSString *)password
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:userName forKey:@"userName"];
    [params setValue:password forKey:@"password"];
    NSString *identifier = [ParameterManager GetDeviceToken];
    [params setValue:identifier forKey:@"deviceTokens"];
    return [self request:@"contact/register" params:params];
}

- (ResultSet *)loginWithWeChat:(NSDictionary *)userInfo
{
    NSDictionary *param = [NSMutableDictionary new];
    [param setValue:userInfo[@"openid"] forKey:@"openid"];
    [param setValue:userInfo[@"nickname"] forKey:@"nickname"];
    [param setValue:userInfo[@"headimgurl"] forKey:@"headimgurl"];
    NSString *identifier = [ParameterManager GetDeviceToken];
    [param setValue:identifier forKey:@"deviceTokens"];
    ResultSet *rs = [self request:@"contact/loginWithWeChat" params:param];

    if (rs && ![rs hasErrors] && ![rs isEmpty])
    {
        AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

        self.contact                           = [rs toDictionary];
        appDelegate.parameterManager.sessionID = self.contact[@"sessionID"];
        [appDelegate.parameterManager save];
    }

    return rs;
}

- (ResultSet *)loginWithWeibo:(NSDictionary *)userInfo
{

    NSDictionary *param = [NSMutableDictionary new];
    [param setValue:userInfo[@"id"] forKey:@"id"];
    [param setValue:userInfo[@"name"] forKey:@"name"];
    [param setValue:userInfo[@"avatar_hd"] forKey:@"avatar_hd"];
    NSString *identifier = [ParameterManager GetDeviceToken];
    [param setValue:identifier forKey:@"deviceTokens"];

    ResultSet *rs = [self request:@"contact/loginWithWeibo" params:param];

    if (rs && ![rs hasErrors] && ![rs isEmpty])
    {
        AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

        self.contact                           = [rs toDictionary];
        appDelegate.parameterManager.sessionID = self.contact[@"sessionID"];
        [appDelegate.parameterManager save];
    }

    return rs;
}

- (ResultSet *)loginWithQQ:(NSDictionary *)userInfo
{
    ResultSet *rs = [self request:@"contact/loginWithQQ" params:userInfo];

    if (rs && ![rs hasErrors] && ![rs isEmpty])
    {
        AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

        self.contact                           = [rs toDictionary];
        appDelegate.parameterManager.sessionID = self.contact[@"sessionID"];

        [appDelegate.parameterManager save];
    }

    return rs;
}

- (ResultSet *)requestIdentifyingCode:(NSString *)cellphone
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:cellphone forKey:@"cellphone"];

    return [self request:@"contact/requestIdentifyingCode" params:params];
}

- (ResultSet *)verifyIdentifyingCode:(NSString *)cellphone identifyingCode:(NSString *)identifyingCode
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:cellphone forKey:@"cellphone"];
    [params setValue:identifyingCode forKey:@"identifyingCode"];
    return [self request:@"contact/verifyIdentifyingCode" params:params];
}

- (ResultSet *)verifyShopName:(NSString *)shopName
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:shopName forKey:@"shopName"];

    return [self request:@"contact/verifyShopName" params:params];
}

- (ResultSet *)uploadShopLogo:(UIImage *)image
{

    NSMutableDictionary *params = [NSMutableDictionary new];

    params[@"sessionID"] = [self getSessionID];

    NSData   *imageData   = UIImageJPEGRepresentation(image, 0.6);
    NSString *imageString = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];//[imageData base64Encoding];
    [params setValue:imageString forKey:@"image"];

    return [self requestJSON:@"contact/uploadShopLogo" params:params];

}

@end